//
//  ISPickerView.m
//  iPhoneSafety
//
//  Created by dlon 2018/12/31.
//  Copyright © 2018年 com.li.ding. All rights reserved.
//

#import "ISPickerView.h"
#import "NWLazyProperty.h"
#import <Masonry/Masonry.h>
#import "UIColor+NWHex.h"
#import <BlocksKit/UIControl+BlocksKit.h>
#import <libextobjc/EXTScope.h>

@interface ISPickerView()<UIPickerViewDelegate,UIPickerViewDataSource>

@property (nonatomic, strong) UIPickerView *picker;
@property (nonatomic, strong) UIButton *bgButton;
@property (nonatomic, strong) NSArray *list;
@end

@implementation ISPickerView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
    if (self) {
        [self addSubview:self.bgButton];
        [self addSubview:self.picker];
        
        [self.picker mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(215.f);
            make.left.right.bottom.equalTo(self);
        }];
        
        [self.bgButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self);
        }];
        @weakify(self)
        [self.bgButton bk_addEventHandler:^(id sender) {
            @strongify(self)
            [self dismiss];
        } forControlEvents:UIControlEventTouchUpInside];
    }
    return self;
}

- (void)showWithList:(NSArray *)list selectIdx:(NSInteger)idx {
    self.list = list;
    [self.picker selectRow:idx inComponent:0 animated:NO];
    [self show];
}

- (void)show {
    [[UIApplication sharedApplication].delegate.window addSubview:self];
    self.bgButton.alpha = 0;
    self.picker.transform = CGAffineTransformMakeTranslation(0, 215.f);
    [UIView animateWithDuration:0.2 animations:^{
        self.bgButton.alpha = 1;
        self.picker.transform = CGAffineTransformIdentity;
    } completion:^(BOOL finished) {
        
    }];
    
}

- (void)dismiss {
    @weakify(self)
    [UIView animateWithDuration:0.2 animations:^{
        @strongify(self)
        self.bgButton.alpha = 0;
        self.picker.transform = CGAffineTransformMakeTranslation(0, 215.f);
    } completion:^(BOOL finished) {
        @strongify(self)
        [self removeFromSuperview];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            @strongify(self)
            
            if (self.completeBlock) {
                NSInteger idx = [self.picker selectedRowInComponent:0];
                self.completeBlock(self.list[idx], idx);
            }
        });
    }];
}

#pragma mark - UIPickerViewDataSource & UIPickerViewDelegate

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return self.list.count;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    if (self.selectBlock) {
        self.selectBlock(self.list[row], row);
    }
}

- (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    NSDictionary *dict = self.list.count > row ? self.list[row] : nil;
    return dict[@"title"];
}


#pragma mark - set & get

- (UIPickerView *)picker {
    if (!_picker) {
        _picker = [UIPickerView new];
        _picker.delegate = self;
        _picker.dataSource = self;
        _picker.backgroundColor = [UIColor whiteColor];
    }
    return _picker;
}


- (UIButton *)bgButton {
    if (!_bgButton) {
        _bgButton = [UIButton new];
        _bgButton.backgroundColor = [UIColor nw_colorWithHex:0x000000 alpha:0.4];
    }
    return _bgButton;
}

@end
